<template>
  <view class="settings-container">
    <!-- 用户信息区域 -->
    <view class="user-info">
      <view class="avatar-wrapper">
        <image :src="userAvatar" class="avatar" mode="aspectFill" />
      </view>
      <view class="user-name">{{ nickName }}</view>
      <view class="login-time">登录时间：{{ loginTimeText }}</view>
    </view>

    <!-- 设置列表 -->
    <view class="settings-list">
      <view class="settings-item" @click="handleLogout">
        <view class="item-content">
          <text class="item-icon">🚪</text>
          <text class="item-text">退出登录</text>
        </view>
        <text class="item-arrow">›</text>
      </view>
    </view>
  </view>
</template>

<script>
const defaultAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

export default {
  data() {
    return {
      userAvatar: defaultAvatar,
      nickName: 'AI助手用户',
      loginTimeText: ''
    };
  },
  onLoad() {
    this.loadUserInfo();
  },
  methods: {
    loadUserInfo() {
      // 获取用户信息
      const avatarUrl = uni.getStorageSync('avatarUrl');
      const nickName = uni.getStorageSync('nickName');
      const loginTime = uni.getStorageSync('loginTime');
      
      if (avatarUrl && (avatarUrl.startsWith('http://') || avatarUrl.startsWith('https://'))) {
        this.userAvatar = avatarUrl;
      }
      
      if (nickName) {
        this.nickName = nickName;
      }
      
      if (loginTime) {
        this.loginTimeText = this.formatTime(new Date(loginTime));
      }
    },
    
    formatTime(date) {
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    },
    
    handleLogout() {
      uni.showModal({
        title: '退出登录',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            // 清除登录信息
            uni.removeStorageSync('isLogin');
            uni.removeStorageSync('nickName');
            uni.removeStorageSync('avatarUrl');
            uni.removeStorageSync('gender');
            uni.removeStorageSync('loginTime');
            
            uni.showToast({
              title: '已退出登录',
              icon: 'success',
              duration: 1500
            });
            
            // 延迟跳转到登录页
            setTimeout(() => {
              uni.reLaunch({
                url: '/pages/login/index'
              });
            }, 1500);
          }
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.settings-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.user-info {
  background: linear-gradient(135deg, #FFF27C 0%, #FFE44D 100%);
  padding: 80rpx 40rpx 60rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  
  .avatar-wrapper {
    width: 160rpx;
    height: 160rpx;
    margin-bottom: 30rpx;
    
    .avatar {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 6rpx solid rgba(255, 255, 255, 0.5);
      box-shadow: 0 8rpx 16rpx rgba(0, 0, 0, 0.1);
    }
  }
  
  .user-name {
    font-size: 36rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 16rpx;
  }
  
  .login-time {
    font-size: 24rpx;
    color: #666;
  }
}

.settings-list {
  margin-top: 20rpx;
  background-color: #fff;
  
  .settings-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 32rpx 40rpx;
    border-bottom: 1rpx solid #f0f0f0;
    transition: background-color 0.2s;
    
    &:active {
      background-color: #f5f5f5;
    }
    
    .item-content {
      display: flex;
      align-items: center;
      gap: 24rpx;
      
      .item-icon {
        font-size: 48rpx;
      }
      
      .item-text {
        font-size: 32rpx;
        color: #333;
      }
    }
    
    .item-arrow {
      font-size: 48rpx;
      color: #999;
      font-weight: 300;
    }
  }
}
</style>
